﻿//using UnityEngine;
//using System.Collections;
//using UnityEditor;
//using System.Collections.Generic;
//using System.Xml;
//using System.IO;
//using System.Text;
//using System.Data;
//using System;

//public class SceneToXMLEditor : Editor
//{
//    private static DataTable table;
//    private static DataTable DefaultSceneTable;
//    //将所有游戏场景导出为XML格式//
//    [MenuItem("GameObject/ExportSceneToXML")]
//    static void ExportXML()
//    {

//        //遍历所有的游戏场景(可以通过点选打包场景来控制)//
//        foreach (UnityEditor.EditorBuildSettingsScene S in UnityEditor.EditorBuildSettings.scenes)
//        {
//            //当关卡启用//
//            if (S.enabled)
//            {
//                try
//                {
//                    //得到关卡的名称//
//                    string ScenePath = S.path;
//                    //打开这个关卡//
//                    EditorApplication.OpenScene(ScenePath);
//                    string[] defaultName = ScenePath.Split(new string[] { "/", "." }, StringSplitOptions.RemoveEmptyEntries);

//                    //////////////////////////////////////////////////////////////////////////

//                    table = new DataTable(defaultName[1]);

//                    DataColumn column = new DataColumn("ID(type:refid)", typeof(System.String));
//                    //column.AutoIncrement = true;
//                    table.Columns.Add(column);

//                    column = new DataColumn("Type(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);

//                    column = new DataColumn("IdIndex(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);

//                    column = new DataColumn("NeedInit(type:int32)", typeof(System.Boolean));
//                    table.Columns.Add(column);

//                    column = new DataColumn("PrefabName(type:string)", typeof(System.String));
//                    table.Columns.Add(column);

//                    column = new DataColumn("ScaleX(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);
//                    column = new DataColumn("ScaleY(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);


//                    column = new DataColumn("PosX(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);
//                    column = new DataColumn("PosY(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);

//                    column = new DataColumn("ColliderPosX(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);
//                    column = new DataColumn("ColliderPosY(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);
//                    column = new DataColumn("ColliderScaleX(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);
//                    column = new DataColumn("ColliderScaleY(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);

//                    column = new DataColumn("FrameSpeed(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);

//                    column = new DataColumn("NamePosX(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);
//                    column = new DataColumn("NamePosY(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);

//                    column = new DataColumn("Name(type:string)", typeof(System.String));
//                    table.Columns.Add(column);

//                    column = new DataColumn("ShadowPosX(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);
//                    column = new DataColumn("ShadowPosY(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);
//                    column = new DataColumn("ShadowScaleX(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);
//                    column = new DataColumn("ShadowScaleY(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);

//                    column = new DataColumn("TaskStatusPosX(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);
//                    column = new DataColumn("TaskStatusPosY(type:int32)", typeof(System.Int32));
//                    table.Columns.Add(column);


//                    //////////////////////////////////////////////////////////////////////////

//                    DefaultSceneTable = new DataTable("Common" + defaultName[1]);

//                    DataColumn columnRes = new DataColumn("CommonID(type:string)", typeof(System.String));
//                    DefaultSceneTable.Columns.Add(columnRes);

//                    columnRes = new DataColumn("CommonPrefabName(type:string)", typeof(System.String));
//                    DefaultSceneTable.Columns.Add(columnRes);

//                    columnRes = new DataColumn("CommonFile(type:string)", typeof(System.String));
//                    DefaultSceneTable.Columns.Add(columnRes);

//                    columnRes = new DataColumn("CommonScaleX(type:int32)", typeof(System.Int32));
//                    DefaultSceneTable.Columns.Add(columnRes);
//                    columnRes = new DataColumn("CommonScaleY(type:int32)", typeof(System.Int32));
//                    DefaultSceneTable.Columns.Add(columnRes);
//                    columnRes = new DataColumn("CommonScaleZ(type:int32)", typeof(System.Int32));
//                    DefaultSceneTable.Columns.Add(columnRes);

//                    columnRes = new DataColumn("CommonPosX(type:int32)", typeof(System.Int32));
//                    DefaultSceneTable.Columns.Add(columnRes);
//                    columnRes = new DataColumn("CommonPosY(type:int32)", typeof(System.Int32));
//                    DefaultSceneTable.Columns.Add(columnRes);
//                    columnRes = new DataColumn("CommonPosZ(type:int32)", typeof(System.Int32));
//                    DefaultSceneTable.Columns.Add(columnRes);

//                    //////////////////////////////////////////////////////////////////////////

//                    // Add rows.
//                    DataRow row;
//                    GameObject parentContainer = GameObject.Find("Panel(NPC)");

//                    foreach (Transform component in parentContainer.GetComponentsInChildren<Transform>())
//                    {
//                        GameObject obj = component.gameObject;

//                        if (obj.name == "Panel(NPC)" || component.parent != parentContainer.transform) continue;

//                        if (obj.name.IndexOf("Npc") != -1)
//                        {
//                            String[] configArr = obj.name.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);

//                            row = table.NewRow();

//                            row["ID(type:refid)"] = configArr[0];
//                            row["Type(type:int32)"] = configArr[1];
//                            row["IdIndex(type:int32)"] = configArr[2];

//                            row["PrefabName(type:string)"] = configArr[1] == "4" ? "Npc" : "Monster";//不同类型，对应不同的预设体//

//                            row["NeedInit(type:int32)"] = obj.activeInHierarchy ? 1 : 0;			//1:true;0:false//

//                            row["ScaleX(type:int32)"] = obj.transform.localScale.x;
//                            row["ScaleY(type:int32)"] = obj.transform.localScale.y;

//                            row["PosX(type:int32)"] = obj.transform.localPosition.x;
//                            row["PosY(type:int32)"] = obj.transform.localPosition.y;

//                            BoxCollider boxColloder = obj.GetComponent<BoxCollider>();
//                            row["ColliderPosX(type:int32)"] = boxColloder.center.x;
//                            row["ColliderPosY(type:int32)"] = boxColloder.center.y;

//                            row["ColliderScaleX(type:int32)"] = boxColloder.size.x;
//                            row["ColliderScaleY(type:int32)"] = boxColloder.size.y;

//                            GameObject SpriteAnimationLayer = obj.transform.FindChild("SpriteAnimationLayer").gameObject;
//                            row["FrameSpeed(type:int32)"] = SpriteAnimationLayer.GetComponent<ClipAnimation>().FPS;

//                            GameObject PlayerName = obj.transform.FindChild("PlayerName").gameObject;
//                            row["NamePosX(type:int32)"] = PlayerName.transform.localPosition.x;
//                            row["NamePosY(type:int32)"] = PlayerName.transform.localPosition.y;

//                            row["Name(type:string)"] = obj.name;

//                            GameObject Shadow = obj.transform.FindChild("Shadow").gameObject;
//                            row["ShadowPosX(type:int32)"] = Shadow.transform.localPosition.x;
//                            row["ShadowPosY(type:int32)"] = Shadow.transform.localPosition.y;

//                            row["ShadowScaleX(type:int32)"] = Shadow.transform.localScale.x;
//                            row["ShadowScaleY(type:int32)"] = Shadow.transform.localScale.y;

//                            GameObject TaskTip = obj.transform.FindChild("TaskTip").gameObject;
//                            row["TaskStatusPosX(type:int32)"] = TaskTip.transform.localPosition.x;
//                            row["TaskStatusPosY(type:int32)"] = TaskTip.transform.localPosition.y;


//                            table.Rows.Add(row);
//                        }
//                        else
//                        {
//                            row = DefaultSceneTable.NewRow();

//                            row["CommonID(type:string)"] = obj.name;

//                            row["CommonPrefabName(type:string)"] = obj.name;

//                            row["CommonFile(type:string)"] = obj.name == "Background" ? obj.GetComponent<MeshRenderer>().sharedMaterial.mainTexture.name : "null";//不同类型，对应不同的资源路径//

//                            row["CommonScaleX(type:int32)"] = obj.transform.localScale.x;
//                            row["CommonScaleY(type:int32)"] = obj.transform.localScale.y;
//                            row["CommonScaleZ(type:int32)"] = obj.transform.localScale.z;

//                            row["CommonPosX(type:int32)"] = obj.transform.localPosition.x;
//                            row["CommonPosY(type:int32)"] = obj.transform.localPosition.y;
//                            row["CommonPosZ(type:int32)"] = obj.transform.localPosition.z;

//                            DefaultSceneTable.Rows.Add(row);
//                        }



//                    }

//                    table.AcceptChanges();
//                    DefaultSceneTable.AcceptChanges();

//                    //////////////////////////////////////////////////////////////////////////

//                    string SavePath = EditorUtility.SaveFilePanel(
//                        "Save ExportFile",
//                        "",
//                        defaultName[1],
//                        "xml");


//                    if (SavePath.Length != 0)
//                    {
//                        if (File.Exists(SavePath))
//                        {
//                            File.Delete(SavePath);
//                        }
//                        table.WriteXml(SavePath, XmlWriteMode.WriteSchema);

//                        EditorUtility.DisplayDialog("Alert!", "Export Scene Success!", "OK");
//                    }

//                    string SaveCommonPath = EditorUtility.SaveFilePanel(
//                        "Save CommonExportFile",
//                        "",
//                        defaultName[1] + "Common",
//                        "xml");


//                    if (SaveCommonPath.Length != 0)
//                    {
//                        if (File.Exists(SaveCommonPath))
//                        {
//                            File.Delete(SaveCommonPath);
//                        }
//                        DefaultSceneTable.WriteXml(SaveCommonPath.ToString(), XmlWriteMode.WriteSchema);

//                        EditorUtility.DisplayDialog("Alert!", "Export CommonScene Success!", "OK");
//                    }

//                }
//                catch (System.Exception ex)
//                {
//                    EditorUtility.DisplayDialog("Error!", ex.ToString(), "OK");
//                }



//                //刷新Project视图， 不然需要手动刷新哦//
//                AssetDatabase.Refresh();
//            }


//        }
//    }
//}